#pragma once
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <stdarg.h>
#include <time.h>
#include <string>
#include <unordered_map>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <pthread.h>
#include <iostream>
#include <unistd.h>
#include <string>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <functional>

#define DEBUG 0
#define NOTICE 1
#define WARINING 2
#define FATAL 3

#define SOCKET_ERR 1
#define BIND_ERR 2
#define LISTEN_ERR 3
#define USAGE_ERR 4
#define CONN_ERR 5

#define BUFFER_SIZE 1024

using namespace std;
const char *log_level[] = {"DEBUG", "NOTICE", "WARINING", "FATAL"};
void logMessage(int level, const char *format, ...)
{
    assert(level >= DEBUG);
    assert(level <= FATAL);
    char *name = getenv("USER");
    char logInfo[1024];
    va_list ap;
    va_start(ap, format);                            // 最后一个知道类型的参数format
    vsnprintf(logInfo, sizeof(logInfo), format, ap); // 以format格式写入logInfo,自动传递参数
    va_end(ap);                                      // ap=NULL
    FILE *out = level == FATAL ? stderr : stdout;
    fprintf(out, "%s | %u | %s | %s\n",
            log_level[level],
            (unsigned int)time(nullptr),
            name == nullptr ? "unknown" : name,
            logInfo);
}
